# 异常
# f = open('test_error.txt')


# try:
#     f = open('test_error.txt')
# except:
#     print('文件不存在')

# try:
#     print(name)
# except NameError as e:
#     print('变量未定义')

# 捕获多个异常
try:
    f = open('test_error.txt')
except (FileNotFoundError, NameError) as e:
    print('文件不存在')
    print(e)
else:
    print("无异常")

try:
    print(2)
except (FileNotFoundError, NameError) as e:
    print('文件不存在')
    print(e)
else:
    print("无异常")

# 全局变量
# global file

f = None
try:
    f = open('test_error.txt')
    # file = open('test.txt')
except Exception:
    print('文件不存在')
    f = open('test.txt')
# 一定会执行的代码逻辑
finally:
    print("finally执行了")
    if f:
        f.close()
